package com.efounder.utils;

import android.util.Log;

import com.efounder.constant.EnvironmentVariable;
import com.efounder.http.EFHttpRequest;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;

/**
 * Created by will on 18-1-24.
 * 商城网络请求
 * @author will
 */

public class MallRequestUtil {
    /**
     * 请求成功字段
     */
    public static final String SUCCESS  = "success";
    private static final String MALL_V2_URL = EnvironmentVariable.getProperty("mallV2Url");

    //积分相关的服务
    private static final String BASE_MALL_BANLANCE = EnvironmentVariable.getProperty("TalkChainUrl") + "/tcserver";;
    //夺宝相关的接口
    private static final String BASE_MALL_TREASURE = EnvironmentVariable.getProperty("mallTreasureUrl")+"/osp-treasure";

    /**
     * 商品base url
     */
    private static final String GOOD_BASE_URL = MALL_V2_URL + "/good";
    /**
     * 获取商品评论
     */
    public static final String GOOD_COMMENT_URL = MALL_V2_URL + "/comment/getGoodComment";
    /**
     * 增加商品评论
     */
    public static final String GOOD_COMMENT_ADD = MALL_V2_URL + "/comment/addComment";
    /**
     * 获取未评价商品
     */
    public static final String GOOD_UNCOMMENT_URL = MALL_V2_URL +"/comment/getNoEvaluationGoodList";
    /**
     * 地址base url
     */
    private static final String ADDRESS_BASE_URL = MALL_V2_URL + "/address";
    /**
     * 地址列表
     */
    public static final String ADDRESS_LIST_URL = ADDRESS_BASE_URL + "/addressList";
    /**
     * 地址删除
     */
    public static final String ADDRESS_DELETE_URL = ADDRESS_BASE_URL + "/deleteAddress ";
    /**
     * 更新地址
     */
    public static final String ADDRESS_UPDATE_URL = ADDRESS_BASE_URL + "/updateAddress";
    /**
     * 新增地址
     */
    public static final String ADDRESS_CREATE_URL = ADDRESS_BASE_URL + "/createAddress";
    /**
     * 订单base url
     */
    private static final String ORDER_BASE_URL = MALL_V2_URL + "/v3/order";
    /**
     * 创建订单
     */
    public static final String ORDER_CREATE_URL = ORDER_BASE_URL + "/createOrder";
    /**
     * 预订单
     */
    public static final String PREPARE_ORDER_URL = ORDER_BASE_URL + "/prepareOrder";
    /**
     * 获取订单列表
     */
    public static final String ORDER_LIST_URL = ORDER_BASE_URL + "/getMyOrder";
    /*
    改变订单状态
    * */
    public static final String CHANGE_ORDER_STATUS_URL = ORDER_BASE_URL + "/changeOrderStatus";
    /**
     * 订单详情
     */
    public static final String ORDER_DETAIL_URL = ORDER_BASE_URL + "/getOrderDetail";
    /**
     * 选择支付
     */
    public static final String ORDER_SELECT_PAY_URL = ORDER_BASE_URL + "/preparePayOrder";
    /**
    /**
     * 夺宝选择支付
     */
    public static final String ORDER_SNATCH_PAY_URL = MALL_V2_URL + "/pay/snatchBeforePay";
    /**
     * 购物车base url
     */
    private static final String CART_BASE_URL = MALL_V2_URL + "/v3/shopCart";
    /**
     * 加入购物车
     */
    public static final String GOOD_ADDTOCART_URL = CART_BASE_URL + "/addToShopCart";
    /**
     * 增删购物车
     */
    public static final String CART_ADD_OR_DELETE_URL = CART_BASE_URL + "/addOrRemoveGood";
    /**
     * 获取购物车数量
     */
    public static final String CART_AMOUNT_URL = CART_BASE_URL + "/getShopCartCount";
    /**
     * 购物车列表
     */
    public static final String CART_LIST_URL = CART_BASE_URL + "/getMyShopCart";
    /**
     * 首页
     */
    public static final String HOME_URL = MALL_V2_URL + "/api/v3/home";
    /**
     * 商品详情
     */
    public static final String GOOD_DETAIL_URL = MALL_V2_URL + "/good/v3/getGoodDetail";
    /**
     * 加入或移除收藏
     */
    public static final String GOOD_COLLECT_AOR = GOOD_BASE_URL + "/collect/addOrRemove";
    /**
     * 我的收藏
     */
    public static final String GOOD_COLLECT_URL = GOOD_BASE_URL + "/collect/collectGoods";
    /**
     * 收藏数量
     */
    public static final String GOOD_COLLECT_NUM = GOOD_BASE_URL + "/collect/collectNumber";
    /**
     * 支付
     */
    public static final String START_PAY_URL = MALL_V2_URL + "/pay/v3/startPay";
    /**
     * 查询个人积分
     */
    public static final String QUERY_USER_BALANCE = BASE_MALL_BANLANCE + "/mallBanlance/getBalanceListByPhone";

    /**
     * 积分扣款
     */
    public static final String PAY_BY_TOKEN_URL = MALL_V2_URL + "/pay/v3/startPayByToken";

    /**
     * 获取夺宝支付的方式和余额
     */
    public static final String MALL_TREASURE_SELECT_PAY_URL = BASE_MALL_TREASURE+"/winTreasure/getBalanceByAccountAddress";

    /**
     * 夺宝商品夺宝详情url
     */
    public static final String MALL_TREASURE_ORDER_DETAILS= BASE_MALL_TREASURE+"/winTreasure/getWinnerInfoByMallGoodsId";




    /**
     *
     * get方法请求
     * @param tag class name
     * @param paramsMap "userId=123"等等
     */
    public static void getRequest(String tag, String url, HashMap<String, String> paramsMap, final MallRequestCallback callback) {
        StringBuilder tempParams = new StringBuilder();
        try {
            int pos = 0;
            for (String key : paramsMap.keySet()) {
                if (pos > 0) {
                    tempParams.append("&");
                }
                tempParams.append(String.format("%s=%s", key, URLEncoder.encode(paramsMap.get(key), "utf-8")));
                pos++;
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        String requestUrl = String.format("%s?%s", url, tempParams.toString());
        EFHttpRequest httpRequest = new EFHttpRequest(tag);
        httpRequest.setHttpRequestListener(new EFHttpRequest.HttpRequestListener() {
            @Override
            public void onRequestSuccess(int requestCode, String response) {
                callback.onSuccess(response);
            }

            @Override
            public void onRequestFail(int requestCode, String message) {
                callback.onFail(message);
            }
        });
        Log.d("MallGetRequest", requestUrl);
        httpRequest.httpGet(requestUrl);
    }

    /**
     *
     * post方法请求
     * @param tag class name
     * @param paramsMap "userId=123"等等
     */
    public static void postRequest(String tag, String url, HashMap<String, String> paramsMap, final MallRequestCallback callback) {
        EFHttpRequest httpRequest = new EFHttpRequest(tag);
        httpRequest.setHttpRequestListener(new EFHttpRequest.HttpRequestListener() {
            @Override
            public void onRequestSuccess(int requestCode, String response) {
                callback.onSuccess(response);
            }

            @Override
            public void onRequestFail(int requestCode, String message) {
                callback.onFail(message);
            }
        });
        httpRequest.httpPost(url, paramsMap);
    }

    public interface MallRequestCallback {

        void onSuccess(String response);

        void onFail(String error);
    }

}
